﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;
using HtmlExtractor;
using HtmlExtractor.Output;
using System.Xml;
using HtmlExtractor.XPathOutput;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml.Linq;
using System.Xml.XPath;
using System.Net;

namespace SlTest
{
   
    [TestClass]
    public class Lxwxw:TestBase
    {
        [TestMethod]
        public void ParserPage()
        {
            string parseUri = @"http://www.lxwxw.com/xiaoshuoinfo/69/69255.html";
            string textUri = @"\Resource\lxwxw\bookpage.txt";
            PageResult result = GetLoacalPageResult(parseUri,textUri);            
            var simples = result.OfType<SimpleItem>();
            var compositGroups = result.OfType<CompositeItem>();
            Assert.AreEqual("《邪龙道》", simples.Where(s => s.OutputPath == "BookPage/BookName").Single().Value);
            Assert.AreEqual("武侠修真", simples.Where(s => s.OutputPath == "BookPage/Catalog").Single().Value);
            Assert.AreEqual("血红", simples.Where(s => s.OutputPath == "BookPage/Author").Single().Value);
            Assert.AreEqual("2010-05-24", simples.Where(s => s.OutputPath == "BookPage/LastUpdateTime").Single().Value);
            Assert.AreEqual(@"他绝对不是一个好人；他绝对不是一个坏人；他的人生信仰是和气生财；一个个上古仙神以一个迥异的方式出现在他身边；一个个奇异、奇怪的人自愿或者不自愿的在他身边聚集；最终，他一飞冲天；", simples.Where(s => s.OutputPath == "BookPage/Introduce").Single().Value);
            Assert.AreEqual("http://www.lxwxw.com/files/article/html/69/69255/index.html", simples.Where(s => s.OutputPath == "BookPage/BookContentUri").Single().Value);
            Assert.AreEqual("http://www.lxwxw.com/files/article/image/69/69255/69255s.jpg", simples.Where(s => s.OutputPath == "BookPage/ImageUri").Single().Value);
            Assert.AreEqual(7, simples.Count());
            Assert.AreEqual(1, compositGroups.Count());
            Assert.AreEqual(@"http://www.lxwxw.com/files/article/html/69/69255/12977887.html", (from s in compositGroups.Single() where s.OutputPath == "BookPage/NewChapterUri" select s.Value).Single());
            Assert.AreEqual("第一百五十八章 星级走私商（上）", (from s in compositGroups.Single() where s.OutputPath == "BookPage/NewChapterName" select s.Value).Single());
            var b = from a in compositGroups
                    let c = a
                    from s in c
                    where s.OutputPath == "BookPage/NewChapterName"
                    select s.Value;
            Assert.AreEqual(1, b.Count());
        }
        [TestMethod]
        public void PageOutput()
        {
            string parseUri = @"http://www.lxwxw.com/xiaoshuoinfo/69/69255.html";
            string textUri = @"\Resource\lxwxw\bookpage.txt";
            PageResult result = GetLoacalPageResult(parseUri, textUri);
            XDocumentOutput output = new XDocumentOutput();
            XDocument doc = XDocument.Load(@"Resource/lxwxw/bookpagetemplate.xml");
            output.Template = doc;
            XDocument rdoc;
            output.TryGetOutPutResult(result,out rdoc);
            Assert.AreEqual("第一百五十八章 星级走私商（上）", rdoc.XPathSelectElement("//NewChapterName").Value);
            //Assert.AreEqual("第一百五十八章 星级走私商（上）", rdoc.XPathSelectElement("//NewChapter").Attribute("name").Value);
            //Assert.AreEqual("http://www.lxwxw.com/files/article/html/69/69255/12977887.html", rdoc.XPathSelectElement("//NewChapter").Attribute("uri").Value);

        }
        [TestMethod]
        public void ContentElmentOutput()
        {
            string parseUri = @"http://www.lxwxw.com/files/article/html/69/69255/index.html";
            string textUri = @"\Resource\lxwxw\content.txt";
            PageResult result = GetLoacalPageResult(parseUri, textUri);
            XDocumentOutput output = new XDocumentOutput();
            XDocument doc = XDocument.Load(@"Resource/lxwxw/bookcontentelementtemplate.xml");
            output.Template = doc;
            XDocument rdoc;
            output.TryGetOutPutResult(result,out rdoc);
            Assert.AreEqual(@"第七章 伏击http://www.lxwxw.com/files/article/html/69/69255/12686858.html", rdoc.XPathSelectElement("//Chapter[8]").Value);
            Assert.AreEqual("第三十五章 疯狂的抵抗http://www.lxwxw.com/files/article/html/69/69255/12707892.html", rdoc.XPathSelectElement("//Chapter[388]").Value);
            Assert.AreEqual(674, rdoc.XPathSelectElements("//Chapter").Count());
        }
        //[TestMethod]
        //public void ContentAttibuteOutput()
        //{
        //    //需要修改
        ////    <Group name="采用替换的产生章节列表">
        ////    <Regex><![CDATA[<A\s*href='(?<uri>.*?)'>(?<name>.*?)</a>]]></Regex>
        ////    <GroupInfo name="uri"  outputPath="Chapter/Uri,//Chapter">	
        ////    <ReplaceExpression>
        ////        <FindRegex><![CDATA[\d+.html]]></FindRegex>
        ////        <ProcessedReplace name="绝对路径" inputPath="PageUri">
        ////            <ReplacementText>{0}$0</ReplacementText>
        ////            <Group>
        ////                    <Regex>(.*?/)index</Regex>
        ////                    <GroupInfo outputPath="absolute"/>
        ////            </Group>											
        ////        </ProcessedReplace>	
        ////        </ReplaceExpression>	
        ////    </GroupInfo>
        ////    <GroupInfo name="name" outputPath="Chapter/Name,//Chapter">			
        ////    </GroupInfo>
        ////</Group>
        //    string parseUri = @"http://www.lxwxw.com/files/article/html/69/69255/index.html";
        //    string textUri = @"\Resource\lxwxw\content.txt";
        //    PageResult result = GetLoacalPageResult(parseUri, textUri);
        //    XmlDocument doc = new XmlDocument();
        //    doc.Load(RootPath + @"\Resource\lxwxw\bookcontenttemplate.xml");            
        //    XmlDocumentOutput output = new XmlDocumentOutput(doc);
        //    XmlDocument rdoc = output.GetOutPutResult(result);
        //    Assert.AreEqual(674, rdoc.SelectNodes("//Chapter").Count);
        //    Assert.AreEqual("第七章 伏击", rdoc.SelectSingleNode("//Chapter[8]").Attributes["name"].Value);
        //    Assert.AreEqual("第三十五章 疯狂的抵抗", rdoc.SelectSingleNode("//Chapter[388]").Attributes["name"].Value);
        //    Assert.AreEqual(2, rdoc.SelectNodes("//BookPage").Count);
        //    Assert.AreEqual(@"http://www.lxwxw.com/modules/article/articleinfo.php?id=69255", rdoc.SelectSingleNode("//BookPage[1]").InnerText);
        //    Assert.AreEqual(@"http://www.lxwxw.com/modules/article/articleinfo.php?id=69255", rdoc.SelectSingleNode("//BookPage[2]").InnerText);
        //}
        [TestMethod]
        public void ParserContent()
        {
            string parseUri = @"http://www.lxwxw.com/files/article/html/69/69255/index.html";
            string textUri = @"\Resource\lxwxw\content.txt";
            PageResult result = GetLoacalPageResult(parseUri, textUri);

            var simples = result.OfType<SimpleItem>();
            var compositGroups = result.OfType<CompositeItem>();
            Assert.AreEqual("作者：血红", simples.Where(s => s.OutputPath == "BookContent/Author").Single().Value);
            Assert.AreEqual("http://www.lxwxw.com/modules/article/articleinfo.php?id=69255", simples.Where(s => s.OutputPath == "BookContent/BookPage").Single().Value);
            Assert.AreEqual("邪龙道", simples.Where(s => s.OutputPath == "BookContent/BookName").Single().Value);
            Assert.AreEqual(337, compositGroups.Count());
            //通过absoluteUriProcessor替换成绝对路径
            var uri = (from c in compositGroups.Last()
                       where c.OutputPath == "Chapter/Uri,//Chapter"
                       select c.Value).Single();
            Assert.AreEqual(@"http://www.lxwxw.com/files/article/html/69/69255/12981816.html", uri);
            ////通过replace替换成绝对路径
            //var uri1 = (from c in compositGroups.First()
            //            where c.OutputPath == "BookContent/ChapterUri"
            //            select c.Value).Single();
            //Assert.AreEqual(@"http://www.lxwxw.com/files/article/html/69/69255/12686851.html", uri1);
        }

        //[TestMethod]
        public void ParserOnlineChapter()
        {
            string parseUri = @"http://www.lxwxw.com/files/article/html/65/65661/12989085.html";
          // string parseUri = @"http://localhost:41954/WebSite3/a.html";
          UriSource.GetOnlineSource(parseUri,down);
        }
        [TestMethod]
        public void ParserChapter()
        {
         
            //string parseUri = @"http://www.lxwxw.com/files/article/html/65/65661/12989085.html";
            //string textUri = @"\Resource\lxwxw\chapter.txt";
            //PageResult result = GetLoacalPageResult(parseUri, textUri);
            //List<SimpleItem> ss = result.Cast<SimpleItem>().ToList();
            //Assert.AreEqual("正文 第七百三十四章 征服大军", ss[0].Value);
            //Assert.AreEqual("index.html", ss[1].Value);
            //Assert.AreEqual("12987020.html", ss[2].Value);
            //Assert.AreEqual("index.html", ss[3].Value);
            //Assert.AreEqual("http://pic.lxwxw.com/65/65661/12989085/7509198.gif", ss[4].Value);
            //Assert.AreEqual("http://pic.lxwxw.com/65/65661/12989085/7509199.gif", ss[5].Value);
        }

        public static void down(object sender, DownloadStringCompletedEventArgs e)
        {
            
            string parseUri = @"http://www.lxwxw.com/files/article/html/65/65661/12989085.html";
            ParserContext context = new ParserContext(e.Result, parseUri);
            string extractorFile = @"Resource/Extractor.config";
            Extractor extr = new Extractor(extractorFile);
            PageResult result;
            extr.TryGetResult(context,out result);
            List<SimpleItem> ss = result.Cast<SimpleItem>().ToList();
            Assert.AreEqual("正文 第七百三十四章 征服大军", ss[0].Value);
            Assert.AreEqual("12991325.html", ss[1].Value);
            Assert.AreEqual("12987020.html", ss[2].Value);
            Assert.AreEqual("index.html", ss[3].Value);
            Assert.AreEqual("http://pic.lxwxw.com/65/65661/12989085/7509198.gif", ss[4].Value);
            Assert.AreEqual("http://pic.lxwxw.com/65/65661/12989085/7509199.gif", ss[5].Value);
        }
    }
}
